<template>
    <view class="settlement">
        <!-- 头部总收益区域 -->
        <view class="header">
            <view class="total-income">
                <text class="label">总收益</text>
                <view class="amount">
                    <text class="currency">¥</text>
                    <text class="number">{{ profitStatistics.total_profit }}</text>
                </view>
            </view>
            <view class="sub-amounts">
                <view class="item">
                    <text>总退款：</text>
                    <text>¥ {{ profitStatistics.total_refund }}</text>
                </view>
                <view class="item">
                    <text>待结算：</text>
                    <text>¥ {{ profitStatistics.pending_settlement }}</text>
                </view>
                <view class="item">
                    <text>待分账：</text>
                    <text>¥ {{ profitStatistics.pending_profit }}</text>
                </view>
            </view>
        </view>

        <!-- 搜索栏 -->
        <view class="search-bar">
            <view class="search-input">
                <text class="iconfont icon-search"></text>
                <input type="text" placeholder="搜索单号" placeholder-class="placeholder" v-model="searchParams.settlement_no"/>
            </view>
            <view class="filter-btn" @click="handleSearch">
                <text class="iconfont icon-filter"></text>
                <text>筛选</text>
            </view>
        </view>

        <!-- 列表标题 -->
        <view class="list-header">
            <text>交易来源</text>
            <text>金额</text>
            <text>交易时间</text>
        </view>

        <!-- 交易列表 -->
        <view class="transaction-list">
            <view class="transaction-item" v-for="(item, index) in settlementList" :key="index">
                <view class="item-main">
                    <text class="source">{{ item.business_type_name }}</text>
                    <text class="amount positive">+{{ item.profit_amount }}</text>
                    <text class="time">{{ item.create_time_text }}</text>
                </view>
                <view class="item-footer">
                    <text class="settlement-status" :class="getStatusClass(item.settlement_status)">
                        {{ item.settlement_status_name }}
                    </text>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
	import { apiProfitStatistics,apiProfitSettlementDetail } from '@/api/order'
export default {
    data() {
        return {
            profitStatistics: {
                total_profit: 0,
                total_refund: 0,
                pending_settlement: 0,	
                pending_profit: 0,
            },
            settlementList: [],
            searchParams: {
                page_no: 1,
                page_size: 5,
                settlement_no: ''
            }
        }
    },
    methods: {
        async getProfitStatistics() {
            const res = await apiProfitStatistics()	
            this.profitStatistics = res		
        },
        async getSettlementList() {
            const res = await apiProfitSettlementDetail(this.searchParams)
            this.settlementList = res.lists
        },
        handleSearch() {
            this.searchParams.page_no = 1
            this.getSettlementList()
        },
        getStatusClass(status) {
            switch (status) {
                case 5: return 'status-success'; // 已分账
                case 0: return 'status-pending'; // 待结算
                case 3: case 6: case 7: return 'status-fail'; // 失败
                case 1: case 2: case 4: return 'status-processing'; // 处理中
                default: return '';
            }
        }
    },
    onLoad() {
        this.getProfitStatistics()
        this.getSettlementList()	
    }

}
</script>

<style lang="scss" scoped>
.settlement {
    min-height: 100vh;
    background-color: #f5f5f5;

    .header {
        background-color: #4CD964;
        padding: 30rpx;
        color: #fff;

        .total-income {
            .label {
                font-size: 28rpx;
                opacity: 0.9;
            }

            .amount {
                margin-top: 20rpx;
                
                .currency {
                    font-size: 40rpx;
                    margin-right: 10rpx;
                }

                .number {
                    font-size: 60rpx;
                    font-weight: 500;
                }
            }
        }

        .sub-amounts {
            margin-top: 30rpx;
            display: flex;
            justify-content: space-between;
            font-size: 24rpx;
            opacity: 0.9;
        }
    }

    .search-bar {
        display: flex;
        align-items: center;
        padding: 20rpx 30rpx;
        background-color: #fff;
        margin: 20rpx 0;

        .search-input {
            flex: 1;
            height: 72rpx;
            background-color: #f5f5f5;
            border-radius: 36rpx;
            display: flex;
            align-items: center;
            padding: 0 30rpx;
            margin-right: 20rpx;

            input {
                flex: 1;
                margin-left: 10rpx;
            }
        }

        .filter-btn {
            display: flex;
            align-items: center;
            color: #666;
            
            .iconfont {
                margin-right: 4rpx;
            }
        }
    }

    .list-header {
        display: flex;
        padding: 20rpx 30rpx;
        background-color: #fff;
        font-size: 28rpx;
        color: #999;
        border-bottom: 1rpx solid #eee;

        text {
            &:nth-child(1) {
                flex: 2;
            }
            &:nth-child(2) {
                flex: 1;
                text-align: center;
            }
            &:nth-child(3) {
                flex: 2;
                text-align: right;
            }
        }
    }

    .transaction-list {
        background: none;
        padding: 20rpx;

        .transaction-item {
            background: #fff;
            border-radius: 16rpx;
            box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
            margin-bottom: 20rpx;
            padding: 30rpx;
            display: flex;
            flex-direction: column;

            .item-main {
                display: flex;
                align-items: center;
                margin-bottom: 16rpx;

                .source { flex: 2; color: #333; }
                .amount { flex: 1; text-align: center; font-weight: bold; }
                .time { flex: 2; text-align: right; color: #999; font-size: 24rpx; }
            }
            .item-footer {
                display: flex;
                justify-content: flex-end;
                .settlement-status {
                    padding: 4rpx 16rpx;
                    border-radius: 20rpx;
                    font-size: 22rpx;
                    font-weight: 500;
                    &.status-success { background: #e6f9ec; color: #27ae60; }
                    &.status-pending { background: #f5f5f5; color: #999; }
                    &.status-fail { background: #ffeaea; color: #e74c3c; }
                    &.status-processing { background: #eaf6ff; color: #3498db; }
                }
            }
        }
    }
}

// 占位符样式
.placeholder {
    color: #999;
    font-size: 28rpx;
}
</style>